package com.boss.common;

import java.text.SimpleDateFormat;

import org.codehaus.jackson.map.DeserializationConfig;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.JavaType;

import com.beidouapp.common.util.DateUtils;

/**
 * Json To Pojo 转换工具。
 *
 * @author mhuang
 */
public class ObjectMapperUtils {

    public static ObjectMapper getObjectMapper() {
        ObjectMapper mapper = new ObjectMapper();
        mapper.setDeserializationConfig(mapper.getDeserializationConfig().withDateFormat(new SimpleDateFormat(DateUtils.FORMAT_LONG)));
        mapper.setSerializationConfig(mapper.getSerializationConfig().withDateFormat(new SimpleDateFormat(DateUtils.FORMAT_LONG)));
        mapper.configure(DeserializationConfig.Feature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
        return mapper;
    }

    public static JavaType getJavaType(ObjectMapper mapper, Class<?> collectionClass, Class<?>... elementClasses) {
        JavaType javaType = mapper.getTypeFactory().constructParametricType(collectionClass, elementClasses);
        return javaType;
    }
}
